home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / c / library / dos / diverses / leda / incl / dp_hashi.h < prev    next >
Encoding:
C/C++ Source or Header  |  1991-11-15  |  4.3 KB  |  218 lines

  1. /*******************************************************************************
  2. +
  3. +  LEDA  2.1.1                                                 11-15-1991
  4. +
  5. +
  6. +  dp_hashing.h
  7. +
  8. +
  9. +  Copyright (c) 1991  by  Max-Planck-Institut fuer Informatik
  10. +  Im Stadtwald, 6600 Saarbruecken, FRG     
  11. +  All rights reserved.
  12. *******************************************************************************/
  13.  
  14.  
  15.  
  16.  
  17.  
  18. //------------------------------------------------------------------------------
  19. // Dynamic Perfect Hashing
  20. //
  21. // Michael Wenzel          ( 1989/90 )
  22. //------------------------------------------------------------------------------
  23.  
  24.  
  25. #ifndef DPHASHINGH
  26. #define DPHASHINGH
  27.  
  28. #include <LEDA/basic.h>
  29.  
  30.  
  31. // ---------------------------------------------------------------
  32. // declarations
  33. // ---------------------------------------------------------------
  34.  
  35.  
  36. class headertable;
  37. class subtable;
  38. class dphash;
  39.  
  40. typedef headertable* htp;
  41. typedef subtable* stp;
  42.  
  43. #define EMPTY (ent(2147483648))
  44. #define maxuni 2147483647
  45.                                 // #define maxprim 2147483659
  46.                 // aber kleiner wegen random-Funktion
  47. #define maxprim 2147483647
  48. #define wursechs 2.44948974278
  49.  
  50. // --------------------------------------------------------------
  51. // class declarations
  52. // --------------------------------------------------------------
  53.  
  54. // --------------------------------------------------------------
  55. // class headertable
  56. //
  57. // Items werden auf Headertables gehasht,
  58. // die sie dann injektiv auf Subtables verteilen
  59.  
  60. class headertable {
  61.  
  62.   unsigned kj;                     // Multiplikator
  63.   int wj;                          // Anzahl Elemente in Tafel
  64.   int mj;                          // max. Anzahl Elemente
  65.   stp tj;                          // Startpunkt der Subtables
  66.  
  67.   friend class b_dict;
  68.   friend class dphash;
  69.   friend class subtable;
  70.  
  71.   stp lookup(ent );
  72.  
  73.   bool insert(ent ,ent ,stp& ,int& ,bool& ,stp ,stp );
  74.   int dinsert(ent ,ent ,stp ,stp& ,stp& );
  75.  
  76.   bool del(ent ,stp ,stp ); 
  77.   int give_elements(stp& ,stp ,stp );  
  78.  
  79.  
  80.   headertable()
  81.     { 
  82.       kj = wj = mj=0; 
  83.       tj=0;   
  84.      }
  85.  
  86.   OPERATOR_NEW(4);
  87.   OPERATOR_DEL(4);
  88.  
  89. };
  90.  
  91. // --------------------------------------------------------------
  92. // class subtable
  93. //
  94. // Jedes Subtableelement ist leer,
  95. // oder von der Headertable wird genau ein Element    
  96. // auf eine Position gehasht
  97.  
  98. class subtable {
  99.  
  100.   ent ke;
  101.   ent inf;
  102.  
  103.   friend class b_dict;
  104.   friend class dphash;
  105.   friend class headertable;
  106.  
  107.   void set_s(ent a,ent b)
  108.     {
  109.        ke=a; inf=b; }
  110.  
  111.   void clear()
  112.     { 
  113.        ke=EMPTY; inf=0; }
  114.  
  115.   subtable()
  116.     {  
  117.        ke=EMPTY; inf=0; }
  118.  
  119.   subtable(ent a ,ent b )
  120.     {  
  121.        ke=a; inf=b; }
  122.  
  123.   subtable(subtable& s)
  124.     {  
  125.        ke=s.ke;
  126.        inf=s.inf; }
  127.  
  128.    subtable& operator=(subtable& s)
  129.     {
  130.        ke=s.ke;
  131.        inf=s.inf;
  132.        return *this; }
  133.  
  134.  OPERATOR_NEW(2);
  135.  OPERATOR_DEL(2);
  136.  
  137.   public:
  138.  
  139.   ent key() { return ke; }
  140.  
  141.   ent info() { return inf; }
  142.  
  143. };
  144.  
  145. // ------------------------------------------------------------------
  146. // class dphash
  147. //
  148. // alle Informationen fuer das Dictionary
  149. // Elemente werden auf Headertables gehasht, 
  150. // die dann die Elemente weitergeben
  151. // Der Platzverbrauch der Headertables wird kontrolliert
  152.  
  153.  
  154. class dphash {
  155.  
  156.   htp* htablep;      // Feld von Zeigern auf Headertables
  157.              // leere Headertables werden nicht angelegt
  158.  
  159.   unsigned k;        // Verteilungsfunktion (k*x)%p%sM
  160.  
  161.   int n;             // Anzahl Elemente im Dictionary
  162.  
  163.   int M;             // Anzahl Elemente, die momentan bzgl.
  164.              // Platzverbrauch mit Wahrscheinlichkeit >= 0.5
  165.              // abgespeichert werden koennen ( ohne Rehash )
  166.  
  167.   int sM;            // Anzahl Headertables
  168.  
  169.   int bed;           // Kontrolle des Platzverbrauchs
  170.  
  171.   stp anf,wo,ende;   // zur Speicherverwaltung beim Rehash
  172.  
  173.  
  174.  
  175.   stp  rehash_all(ent ,ent );
  176.  
  177.  
  178.   virtual void clear_key(ent&) {}
  179.   virtual void clear_inf(ent&) {}
  180.  
  181.   virtual void copy_key(ent&)  {}
  182.   virtual void copy_inf(ent&)  {}
  183.  
  184. public:
  185.  
  186.   ent  key(stp it)
  187.     { return ( it ) ? it->key() : 0 ; }
  188.  
  189.   ent& info(stp it)
  190.     { if (!it) error_handler(1,"dp_item gleich nil");
  191.       return it->inf; }
  192.  
  193.   stp  insert(ent ,ent );
  194.   void del(ent );
  195.  
  196.   int  member(ent );
  197.   stp  lookup(ent );
  198.  
  199.   stp  change_inf(ent ,ent );
  200.  
  201.   int  size()
  202.     { return n; }
  203.  
  204.   void clear();
  205.  
  206.  
  207.   dphash();
  208.   dphash(int ,ent* ,ent* );
  209.   ~dphash();
  210.  
  211.  
  212.   friend class b_dict;
  213.  
  214. };
  215.  
  216. #endif DPHASHINGH
  217.